/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author TranDangThuan
 */
public class PropertiesUtility {

    public static File propDBFile = new File(System.getProperty("user.dir") + "\\config.properties");
    public static File propUserFile = new File(System.getProperty("user.dir") + "\\user.properties");
    public static Properties propDB = new Properties();
    public static Properties propUser = new Properties();

    public static void createDBProperties(String[] propValues) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(System.getProperty("user.dir") + "\\config.properties");
            propDB.setProperty("server", propValues[0]);
            propDB.setProperty("port", propValues[1]);
            propDB.setProperty("db", propValues[2]);
            propDB.setProperty("user", propValues[3]);
            propDB.setProperty("pass", propValues[4]);
            propDB.setProperty("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
            propDB.store(fos, null);
            fos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void loadDBProperties() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(System.getProperty("user.dir") + "\\config.properties");
            propDB.load(fis);
            fis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static String[] getDBProperties() {
        if (propDBFile.exists()) {
            if (propDB.isEmpty()) {
                loadDBProperties();
            }

            String[] values = new String[5];
            values[0] = propDB.getProperty("server");
            values[1] = propDB.getProperty("port");
            values[2] = propDB.getProperty("db");
            values[3] = propDB.getProperty("user");
            values[4] = propDB.getProperty("pass");
            return values;
        } else {
            return new String[5];
        }
    }

    public static void createUserProperties(String[] propValues) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(System.getProperty("user.dir") + "\\user.properties");
            propUser.setProperty("user", propValues[0]);
            propUser.setProperty("pass", propValues[1]);
            propUser.setProperty("ID", propValues[2]);
            propUser.store(fos, null);
            fos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void loadUserProperties() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(System.getProperty("user.dir") + "\\user.properties");
            propUser.load(fis);
            fis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static String[] getUserProperties() {
        if (propUserFile.exists()) {
            loadUserProperties();

            String[] values = new String[3];
            values[0] = propUser.getProperty("user");
            values[1] = propUser.getProperty("pass");
            values[2] = propUser.getProperty("ID");
            return values;
        } else {
            return new String[2];
        }
    }
}
